package com.elinkway.infinitemovies.async;

import android.content.Context;
import android.os.Bundle;

import com.elinkway.infinitemovies.bean.AdNetWorkTypeInterface;
import com.elinkway.infinitemovies.bean.NewAdDataBean;
import com.elinkway.infinitemovies.bean.OnePointDataBean;
import com.elinkway.infinitemovies.bigdata.BigDataConstants;
import com.elinkway.infinitemovies.bigdata.BigDataUtils;
import com.elinkway.infinitemovies.dao.PreferencesManager;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.NewAdDataParser;
import com.elinkway.infinitemovies.http.parser.OnePointDataParser;
import com.elinkway.infinitemovies.utils.MoviesUtils;
import com.elinkway.infinitemovies.utils.MyLocationManager;
import com.elinkway.infinitemovies.utils.NetWorkTypeUtils;
import com.elinkway.infinitemovies.utils.PhoneUtils;
import com.elinkway.infinitemovies.utils.SharePreferenceManager;
import com.elinkway.infinitemovies.utils.StringUtil;
import com.elinkway.infinitemovies.utils.UIs;
import com.elinkway.infinitemovies.utils.Utils;
import com.lvideo.http.bean.LVideoDataHull;

import java.util.Locale;

/**
 * Created by lxj on 2017/8/23.
 *
 * 一点资讯列表请求任务
 */

public class RequestOnepointListTask extends
        MoviesHttpAsyncTask<OnePointDataBean> {

    private Context mContext;

    private int historycount;
    private String action = "refesh";

    public int getHistorycount() {
        return historycount;
    }

    public void setHistorycount(int historycount) {
        this.historycount = historycount;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    private RequestResultListener<OnePointDataBean> mListener;
    public RequestOnepointListTask(Context context, RequestResultListener<OnePointDataBean> listener) {
        super(context);
        this.mContext = context;
        this.mListener = listener;
    }

    public interface AD_PARAMS {
        String USER_ID = "userid";
        String ACTION = "action";
        String COUNT = "count";
        String HISTORYCOUNT = "historyCount";
        String HISTORYTIMESTAMP = "historyTimestamp";
        String NET = "net";
        String PLATFORM = "platform";
        String MAC = "mac";
        String IMEI = "imei";
        String IP = "ip";
        String APPVERSION = "appVersion";
        String REGION = "region";
        String CITYCODE = "cityCode";
        String SCREENHEIGHT = "screenHeight";
        String SCREENWIDTH = "screenWidth";
        String DEVICE = "device";
        String ANDRIODVERSION = "androidVersion";
    }

    private Bundle buildCommonParams() {
        Bundle params = new Bundle();
        params.putString(AD_PARAMS.USER_ID, BigDataConstants.AUID);
        params.putString(AD_PARAMS.ACTION, action);
        params.putString(AD_PARAMS.COUNT, "10");
        params.putString(AD_PARAMS.PLATFORM, "android");
        params.putString(AD_PARAMS.HISTORYCOUNT, historycount+"");
        params.putString(AD_PARAMS.HISTORYTIMESTAMP, System.currentTimeMillis()+"");
        params.putString(AD_PARAMS.NET, BigDataUtils.getNetType(mContext));
        params.putString(AD_PARAMS.MAC, BigDataConstants.MAC);
        params.putString(AD_PARAMS.IMEI, BigDataConstants.MD5IMEI);
        params.putString(AD_PARAMS.IP, PreferencesManager.getInstance().getPublicIp());
        params.putString(AD_PARAMS.APPVERSION, MoviesUtils.getClientVersionName());
        params.putString(AD_PARAMS.REGION, SharePreferenceManager.getVaule(mContext, MyLocationManager.LOCATION_CITY, ""));
        params.putString(AD_PARAMS.CITYCODE, SharePreferenceManager.getVaule(mContext, MyLocationManager.LOCATION_CITY_CODE, ""));
        params.putString(AD_PARAMS.SCREENWIDTH, String.valueOf(UIs.getScreenWidth()));
        params.putString(AD_PARAMS.SCREENHEIGHT, String.valueOf(UIs.getScreenHeight()));
        params.putString(AD_PARAMS.DEVICE, MoviesUtils.getDeviceModel());
        params.putString(AD_PARAMS.ANDRIODVERSION, MoviesUtils.getOSVersion());
        return params;
    }

    @Override
    public void netNull() {
        if (null != mListener) {
            mListener.onRequestFailed();
        }
        super.netNull();
    }

    @Override
    public void dataNull(int updateId, String errMsg) {
        if (null != mListener) {
            mListener.onRequestFailed();
        }
        super.dataNull(updateId, errMsg);
    }

    @Override
    public void netErr(int updateId, String errMsg) {
        if (null != mListener) {
            mListener.onRequestFailed();
        }
        super.netErr(updateId, errMsg);
    }

    @Override
    public boolean onPreExecute() {
        return super.onPreExecute();
    }

    @Override
    public LVideoDataHull<OnePointDataBean> doInBackground() {
        return MoviesHttpApi.requestOnePointList(new OnePointDataParser(), buildCommonParams());
    }

    @Override
    public void onPostExecute(int updateId, OnePointDataBean result) {
        if (null != mListener) {
            mListener.onRequestSuccess(updateId, result);
        }
    }

    public void setListener(RequestResultListener<OnePointDataBean> listener) {
        mListener = listener;
    }
}
